package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.incar.chuxing.R;
import com.incar.chuxing.activity.WebViewActivity;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;

import java.util.ArrayList;


public class CountDownDialog extends Dialog {
    private Context mContext;
    private ImageButton postClose;
    private ConvenientBanner convenientBanner;
    private ArrayList<String> imageList, urlList, titleList;


    public CountDownDialog(Context context, ArrayList<String> imageList, ArrayList<String> urlList, ArrayList<String> titleList) {
        super(context, R.style.MyDialog);
        mContext = context;
        this.imageList = imageList;
        this.urlList = urlList;
        this.titleList = titleList;

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_post);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);
        postClose = (ImageButton) findViewById(R.id.post_close);

        postClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, imageList)
                .setPointViewVisible(false)    //设置指示器是否可见
                .stopTurning();//关闭自动切换
        convenientBanner.setCanLoop(false);
        convenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!TextUtils.isEmpty(urlList.get(position))) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, titleList.get(position));
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, urlList.get(position));
                    mContext.startActivity(intent);
                }
            }
        });
    }


    public class NetworkImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            //你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            ImageLoader.getInstance().displayRoundImage(mContext.getApplicationContext(), 10, data, imageView);
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        //Dialog 全屏
        //  getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
}